// express处理静态文件
// 需要下载另一个第三方模块express-static
let sever=require("express")()//注意要调用，加括号
let expressStatic=require("express-static")
// 规划一个数据库连接
let mysql= require("mysql")
let sql = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '123456',
  database : 'studentmange',
  timezone:'08:00'
});
// 尝试连接
sql.connect();
//发送路由请求
sever.get(`/load`,(request,response)=>{
    // 出现的问题：前端的请求地址冒号写成了中文格式的
    // console.log(request.query)
    // 操作数据库
    sql.query(`SELECT * FROM information`,(error,data)=>{
        if(!error){
            response.send(data)
            console.log(data)
        }
        else{
            response.send(error)
            console.log(error)
        }
    })
})
sever.get(`/delete`,(request,response)=>{
    sql.query(`DELETE FROM information WHERE id=${request.query.id}`,(error,data)=>{
        if(!error){
            response.send("success")
            console.log("删除成功")
        }
        else{
            response.send("error")
            console.log("删除失败")
        }
    })
})
sever.get(`/add`,(request,response)=>{
    console.log(request.query)
    sql.query(`INSERT INTO information (name,age,sex,city,joinDate) VALUES ("${request.query.name}","${request.query.age}","${request.query.sex}","${request.query.city}","${request.query.joinDate}")`,(error,data)=>{
        if(!error){
            response.send("success")
            console.log("新增学生成功")
        }
        else{
            response.send("error")
            console.log("新增学生失败")
        }
    })
})
sever.get(`/update`,(request,response)=>{
    console.log(request.query)
    sql.query(`UPDATE information SET name="${request.query.newname}",age="${request.query.newage}",sex="${request.query.newsex}",city="${request.query.newcity}",joinDate="${request.query.newjoinDate}" WHERE id="${request.query.id}"`,(error,data)=>{
        if(!error){
            response.send("success")
            console.log("修改学生成功")
        }
        else{
            response.send("error")
            console.log("修改学生失败")
        }
    })
})
// 注册请求
sever.get(`/adduser`,(request,response)=>{
    sql.query(`INSERT INTO user (username,password,realname) VALUES ("${request.query.username}","${request.query.password}","${request.query.realname}")`,(error,data)=>{
        if(!error){
            response.send("success")
            console.log("注册成功")
        }
        else{
            response.send("error")
            console.log("注册失败")
        }
    })
})
// 登录请求
sever.get(`/getuser`,(request,response)=>{
    sql.query(`SELECT * FROM user WHERE username="${request.query.username}" AND password="${request.query.password}"`,(error,data)=>{
        if(!error){
            response.send(data)
            console.log(data)
        }
        else{
            response.send(error)
            console.log(error)
        }
    })
})
const port=8080
sever.listen(port)//永久监听开放端口
console.log(`running at ${port}`)
console.log(expressStatic)
sever.use(expressStatic(__dirname+`/static`))//express处理静态请求，调用express的use方法，__dirname代表请求文件的路径
// http://10.35.170.247:8080/static.html,注意访问时需要输入端口号，因为默认的80端口已经被占了